<?php
/**
 * Author: lf
 * Blog: https://blog.szdma.com
 * Email: job@szdma.com
 * Created at: 2016-03-23 17:51
 */

/**
 * @var $dataProvider yii\data\ActiveDataProvider
 * @var $searchModel backend\models\search\ArticleSearch
 */

use yii\helpers\Html;
use yii\widgets\Pjax;
use common\libs\Constants;
use backend\widgets\JsBlock;
use backend\assets\AppAsset;
use backend\assets\plugins\LayerAsset;
use backend\assets\plugins\ICheckAsset;
use kartik\grid\GridView;
use kartik\grid\CheckboxColumn;
use backend\grid\ActionColumn;
use backend\grid\SwitcherColumn;
use backend\grid\SortColumn;
use backend\grid\StatusColumn;
use backend\widgets\Bar;

AppAsset::register($this);
LayerAsset::register($this);
ICheckAsset::register($this);

$this->title                   = 'Articles';
$this->params['breadcrumbs'][] = yii::t('app', 'Articles');
?>
<div class="article-index">
    <div class="box box-primary">
        <div class="box-body">
            <div class="row">
                <div class="col-sm-4 m-b-sm" style="line-height: 2.3;">
                    <?= Bar::widget(['template' => '{create} {delete} {refresh} {view} {comment}']); ?>
                </div>
                <div class="col-sm-8 pull-right text-right">
                    <?php echo $this->render('_search', ['model' => $searchModel]); ?>
                </div>

            </div>
            <?php
            $layout = <<<HTML
<div class="row">
    <div class="table-responsive">{items}</div>
</div>
<div class="row">
  <!--<div class="col-sm-4" style="line-height: 2.3;">-->
    <!--<div data-toggle="buttons" class="btn-group">-->
        <!--<label class="btn btn-sm btn-white"><input type="radio" id="option3" name="options">删除</label>-->
        <!--<label class="btn btn-sm btn-white"><input type="radio" id="option3" name="options">发布</label>-->
    <!--</div>-->
  <!--</div>-->
  <div class="col-sm-4 text-right pull-right " style="line-height: 2.3;">{pager}{summary}</div>
</div>
HTML;
            Pjax::begin(['id' => 'GridView']);
            echo GridView::widget(
                [
                    'dataProvider'   => $dataProvider,
                    'pager'          => ['options' => ['class' => 'pagination pull-right col-sm m-l', 'tag' => 'div']],
                    'summaryOptions' => ['class' => 'h5 m-r', 'tag' => 'span'],
                    'layout'         => $layout,
                    'tableOptions'   => ['class' => 'table table-striped '],
                    'columns'        => [
                        [
                            'class' => CheckboxColumn::className(),
                        ],
                        [
                            'attribute' => 'id',
                            'label'     => yii::t('app', 'ID'),
                        ],
                        [
                            'attribute' => 'title',
                        ],
                        [
                            'attribute' => 'article_category_id',
                            'label'     => yii::t('app', 'Category'),
                            'value'     => function ($model) {
                                return $model->articleCategory ? $model->articleCategory->name : yii::t('app', 'uncategoried');
                            },
                        ],
                        [
                            'attribute' => 'author_name',
                        ],
                        [
                            'class'     => SwitcherColumn::className(),
                            'formName'  => 'Article',
                            'attribute' => 'status',
                        ],
                        [
                            'class'     => SwitcherColumn::className(),
                            'formName'  => 'Article',
                            'attribute' => 'flag_headline',
                        ],
                        [
                            'class'     => SwitcherColumn::className(),
                            'formName'  => 'Article',
                            'attribute' => 'flag_recommend',
                        ],
                        [
                            'attribute' => 'sort',
                            'class' => SortColumn::className(),
                            'label' => yii::t('app', 'Sort'),
                        ],
                        [
                            'attribute' => 'thumb',
                            'format'    => 'raw',
                            'value'     => function ($model, $key, $index, $column) {
                                if ($model->thumb == '') {
                                    $num = Constants::YesNo_No;
                                } else {
                                    $num = Constants::YesNo_Yes;
                                }

                                return Html::a(
                                    Constants::getYesNoItems($num),
                                    $model->thumb ? $model->thumb : 'javascript:void(0)',
                                    [
                                        'img'    => $model->thumb ? $model->thumb : '',
                                        'class'  => 'thumbImg',
                                        'target' => '_blank',
                                    ]
                                );
                            },
                        ],
                        [
                            'class'         => ActionColumn::className(),
                            'template'      => '{view} {update} {delete} {comment}',
                            'headerOptions' => ['style' => 'width:10%;'],
                        ],
                    ],
                    'pjax'           => true,
                    'pjaxSettings'   => [
                        'neverTimeout'    => true,
                        'loadingCssClass' => false,
                    ],
                ]
            ); ?>
            <?php Pjax::end(); ?>
        </div>
    </div>
</div>

<?php JsBlock::begin() ?>
<script>

  $(document).ready(function () {
    var t
    $('table tr td a.thumbImg').hover(showImg, function () {
      clearTimeout(t)
    })
    iCheck()
  })

  function showImg () {
    t = setTimeout(function () {
    }, 200)
    var url = $(this).attr('img')
    if (url.length == 0) {
      layer.tips('<?=yii::t('app', 'No picture')?>', $(this))
    }
    else {
      layer.tips('<img style="max-width: 100px;max-height: 60px" src=' + url + '>', $(this))
    }
  }

  var container = $('#pjax')
  container.on('pjax:send', function (args) {
    layer.load(2)
  })
  container.on('pjax:complete', function (args) {
    iCheck()
    layer.closeAll('loading')
    $('table tr td a.thumbImg').bind('mouseover mouseout', showImg)
    $('input.sort').bind('blur', indexSort)
  })

  function iCheck () {
    $('input[name=\'selection[]\'],[name=\'selection_all\']').iCheck({
      handle: 'checkbox',
      checkboxClass: 'icheckbox_square-green ',
      radioClass: 'iradio_square-green',
    })
    //全选
    $('.select-on-check-all').on('ifChecked', function (event) {
      $('input[name=\'selection[]\']').iCheck('check')
    })
    //反选
    $('.select-on-check-all').on('ifUnchecked', function (event) {
      $('input[name=\'selection[]\']').iCheck('uncheck')
    })
  }
</script>
<?php JsBlock::end() ?>
